{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-01-02T10:20:23.437191Z",
     "end_time": "2024-01-02T10:20:25.969145Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "import math\n",
    "from  sseHp import Hyperparameter\n",
    "hp = Hyperparameter()\n",
    "import sys  # 导入sys模块\n",
    "sys.setrecursionlimit(10000)  # 将默认的递归深度修改为3000\n",
    "\n",
    "class Creator:\n",
    "    # def __init__(self):\n",
    "    #     pass\n",
    "    # def data_create(self):\n",
    "    #     Items = []\n",
    "    #     # np.random.seed(0) # 随机固定\n",
    "    #     # 数据生成变成了12000\n",
    "    # # torch.manual_seed(1)\n",
    "    #     for i in range(12000):#12000\n",
    "    #         if i<1800:#180\n",
    "    #             x,y,z = 4.,4.,3.\n",
    "    #         elif i<3600:#360\n",
    "    #             x,y,z = 6.,4.,3.\n",
    "    #         elif i<5400:#540\n",
    "    #             x,y,z = 6.,3.,3.\n",
    "    #         elif i<7200:#720\n",
    "    #             x,y,z = 6.,3.,2.\n",
    "    #         elif i<9000:#900\n",
    "    #             x,y,z = 4.,3.,2.\n",
    "    #         elif i<10800:#1080\n",
    "    #             x,y,z = 4.,3.,3.\n",
    "    #         elif i<12000:#1200\n",
    "    #             x,y,z = 4.,2.,2. # 4,2,2\n",
    "    #         Items.append([i, x, y, z])\n",
    "    #     Items_noindex = list(Items.copy())\n",
    "    #     Items_noindex = np.array(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex[:, 1:] # 原1:-1\n",
    "    #     Items_noindex = torch.tensor(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex.unsqueeze(1)\n",
    "    #     return Items_noindex\n",
    "    #   shape[1200,1,3] batch,emb,length\n",
    "    # def data_create_random_profit(self):\n",
    "    #     Items = []\n",
    "    #     size = hp.TOTAL_ITEMS_SUM\n",
    "    #     for i in range(size): #\n",
    "    #         t=np.random.randint(20,150)\n",
    "    #         if i< 0.15 * size:\n",
    "    #             x,y,z = 4.,4.,3.\n",
    "    #         elif i<0.3 * size:\n",
    "    #             x,y,z = 6.,4.,3.\n",
    "    #         elif i<0.45 * size:\n",
    "    #             x,y,z = 6.,3.,3.\n",
    "    #         elif i<0.6 * size:\n",
    "    #             x,y,z = 6.,3.,2.\n",
    "    #         elif i<0.75 * size:\n",
    "    #             x,y,z = 4.,3.,2.\n",
    "    #         elif i<0.9 * size:\n",
    "    #             x,y,z = 4.,3.,3.\n",
    "    #         elif i<size:\n",
    "    #             x,y,z = 4.,2.,2.\n",
    "    #         Items.append([i, x, y, z, t])\n",
    "    #     Items_noindex = list(Items.copy())\n",
    "    #     Items_noindex = np.array(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex[:, 1:] # 原1:-1\n",
    "    #     Items_noindex = torch.tensor(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex.unsqueeze(1)\n",
    "    #     return Items_noindex\n",
    "\n",
    "    # def data_create_random_profit_asu(self):\n",
    "    #     Items = []\n",
    "    #     size = hp.TOTAL_ITEMS_SUM\n",
    "    #     for i in range(size):  #\n",
    "    #         if i < 0.15 * size:\n",
    "    #             x, y, z = 4., 4., 3.\n",
    "    #         elif i < 0.3 * size:\n",
    "    #             x, y, z = 6., 4., 3.\n",
    "    #         elif i < 0.45 * size:\n",
    "    #             x, y, z = 6., 3., 3.\n",
    "    #         elif i < 0.6 * size:\n",
    "    #             x, y, z = 6., 3., 2.\n",
    "    #         elif i < 0.75 * size:\n",
    "    #             x, y, z = 4., 3., 2.\n",
    "    #         elif i < 0.9 * size:\n",
    "    #             x, y, z = 4., 3., 3.\n",
    "    #         elif i < size:\n",
    "    #             x, y, z = 4., 2., 2.\n",
    "    #         Items.append([i, x, y, z])\n",
    "    #     Items_noindex = list(Items.copy())\n",
    "    #     Items_noindex = np.array(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex[:, 1:]  # 原1:-1\n",
    "    #     Items_noindex = torch.tensor(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex.unsqueeze(1)\n",
    "    #     return Items_noindex\n",
    "\n",
    "    # def data_creat_save(self):\n",
    "    #     bins=self.data_create()\n",
    "    #     torch. save(bins,'goods_data.pt')\n",
    "    # def data_creat_save_random_profit(self):\n",
    "    #     bins=self.data_create_random_profit()\n",
    "    #     torch. save(bins,'goods_data_random_profit.pt')\n",
    "    # def data_creat_save_random_profit_asu(self):\n",
    "    #     bins=self.data_create_random_profit()\n",
    "    #     torch. save(bins,'goods_data_random_profit_asu.pt')\n",
    "    # def data_create_profit_actual(self):\n",
    "    #     Items = []\n",
    "    #     size = hp.TOTAL_ITEMS_SUM\n",
    "    #     for i in range(size): #\n",
    "    #         if i < 0.1 * size:\n",
    "    #             x,y,z,t = 4.,3.,3.,5. # 5\n",
    "    #         elif i < 0.1 * size:\n",
    "    #             x,y,z,t = 3.,3.,3.,5. # 5\n",
    "    #         elif i < 0.1 * size:\n",
    "    #             x,y,z,t = 3.,3.,2.,5. # 5\n",
    "    #         elif i < size:\n",
    "    #             x,y,z,t = 3.,2.,2.,5. # 5\n",
    "    #         Items.append([i, x, y, z, t])\n",
    "    #     Items_noindex = list(Items.copy())\n",
    "    #     Items_noindex = np.array(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex[:, 1:]  # 原1:-1\n",
    "    #     Items_noindex = torch.tensor(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex.unsqueeze(1)\n",
    "    #     return Items_noindex\n",
    "\n",
    "    def data_creat_save_profit_actual(self):\n",
    "        bins = self.data_create_profit_actual()\n",
    "        torch.save(bins, 'goods_data_profit_actual.pt')\n",
    "    def data_create_profit_actual(self):\n",
    "        Items = []\n",
    "        size = hp.TOTAL_ITEMS_SUM\n",
    "        for i in range(size): #\n",
    "            tt = float(torch.round(torch.tensor(np.random.random_sample(1)), decimals=4))\n",
    "            if i < 200:#200\n",
    "                t = tt * 10. + 50. #50-60\n",
    "                x, y, z, t, family = 4., 3., 3., t, 1\n",
    "                Items.append([i, x, y, z, t, family])\n",
    "            elif i < 600: #\n",
    "                t = tt * 10. + 40. #40-50\n",
    "                x, y, z, t, family = 3., 3., 3., t, 2\n",
    "                Items.append([i, x, y, z, t, family])\n",
    "            elif i < 1100: #\n",
    "                t = tt * 10. + 30. #30-40\n",
    "                x, y, z, t, family = 3., 2., 2., t, 3\n",
    "                Items.append([i, x, y, z, t, family])\n",
    "            elif i < 1800:#1800\n",
    "                t = tt * 10. + 20. #20-30\n",
    "                x, y, z, t, family = 2., 2., 2., t, 4\n",
    "                Items.append([i, x, y, z, t, family])\n",
    "        Items_noindex = list(Items.copy())\n",
    "        Items_noindex = np.array(Items_noindex)\n",
    "        Items_noindex = Items_noindex[:, :]  # 原1:-1\n",
    "        Items_noindex = torch.tensor(Items_noindex)\n",
    "        Items_noindex = Items_noindex.unsqueeze(1)\n",
    "        return Items_noindex\n",
    "\n",
    "    # def data_create_profit_actual(self):\n",
    "    #     Items = []\n",
    "    #     size = hp.TOTAL_ITEMS_SUM\n",
    "    #     for i in range(size): #\n",
    "    #         # t = np.random.randint(20.,100.)\n",
    "    #         if i < 100:#200\n",
    "    #             t = float(np.random.random_sample(1)) * 10. + 40. #50-60\n",
    "    #             x, y, z, t, family = 4., 4., 4., t, 1\n",
    "    #             Items.append([i, x, y, z, t, family])\n",
    "    #         elif i < 200:\n",
    "    #             t = float(np.random.random_sample(1)) * 10. + 30. #40-50\n",
    "    #             x, y, z, t, family = 4., 4., 3.5, t, 2\n",
    "    #             Items.append([i, x, y, z, t, family])\n",
    "    #         elif i < 300:\n",
    "    #             t = float(np.random.random_sample(1)) * 10. + 20. #30-40\n",
    "    #             x, y, z, t, family = 4., 4., 3., t, 3\n",
    "    #             Items.append([i, x, y, z, t, family])\n",
    "    #         elif i < 592:\n",
    "    #             t = float(np.random.random_sample(1)) * 10. + 10. #20-30\n",
    "    #             x, y, z, t, family = 4., 3.5, 3., t, 4\n",
    "    #             Items.append([i, x, y, z, t, family])\n",
    "    #     Items_noindex = list(Items.copy())\n",
    "    #     Items_noindex = np.array(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex[:, :]  # 原1:-1\n",
    "    #     Items_noindex = torch.tensor(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex.unsqueeze(1)\n",
    "    #     return Items_noindex\n",
    "\n",
    "    # def data_creat_save_random_profit_actual(self):\n",
    "    #     bins = self.data_create_profit_actual()\n",
    "    #     torch.save(bins, 'goods_data_random_profit_actual.pt')\n",
    "\n",
    "    # def data_create_all_random_actual(self):\n",
    "    #     Items = []\n",
    "    #     size = hp.TOTAL_ITEMS_SUM\n",
    "    #     for i in range(size):  #\n",
    "    #         # t = np.random.randint(20.,100.)\n",
    "    #         if i < 300:  # 200\n",
    "    #             t = float(np.random.random_sample(1)) * 10. + 50.\n",
    "    #             l = float(np.random.random_sample(1)) + 3.\n",
    "    #             w = float(np.random.random_sample(1)) + 2.\n",
    "    #             h = float(np.random.random_sample(1)) + 2.\n",
    "    #             # 50-60\n",
    "    #             family = 1\n",
    "    #             Items.append([i, l, w, h, t, family])\n",
    "    #         elif i < 700:\n",
    "    #             t = float(np.random.random_sample(1)) * 10. + 40.  # 40-50\n",
    "    #             l = float(np.random.random_sample(1)) + 2.\n",
    "    #             w = float(np.random.random_sample(1)) +2.\n",
    "    #             h = float(np.random.random_sample(1)) +2.\n",
    "    #             family = 2\n",
    "    #             Items.append([i, l, w, h, t, family])\n",
    "    #         elif i < 1300:\n",
    "    #             t = float(np.random.random_sample(1)) * 10. + 30.  # 30-40\n",
    "    #             l = float(np.random.random_sample(1)) +2.\n",
    "    #             w = float(np.random.random_sample(1)) +1.\n",
    "    #             h = float(np.random.random_sample(1)) +1.\n",
    "    #             family = 3\n",
    "    #             Items.append([i, l, w, h, t, family])\n",
    "    #         elif i < 2400:\n",
    "    #             t = float(np.random.random_sample(1)) * 10. + 20.  # 20-30\n",
    "    #             l = float(np.random.random_sample(1)) +2.\n",
    "    #             w = float(np.random.random_sample(1)) +1.\n",
    "    #             h = float(np.random.random_sample(1)) +1.\n",
    "    #             family = 4\n",
    "    #             Items.append([i, l, w, h, t, family])\n",
    "    #     Items_noindex = list(Items.copy())\n",
    "    #     Items_noindex = np.array(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex[:, 1:]  # 原1:-1\n",
    "    #     Items_noindex = torch.tensor(Items_noindex)\n",
    "    #     Items_noindex = Items_noindex.unsqueeze(1)\n",
    "    #     return Items_noindex\n",
    "    #\n",
    "    # def data_creat_save_random_profit_actual(self):\n",
    "    #     bins = self.data_create_random_profit_actual()\n",
    "    #     torch.save(bins, 'goods_data_random_profit_actual.pt')\n",
    "\n",
    "    # 只为强化学习算法而生\n",
    "    def bin_create(self, item_list, index_list): # item 为array,为得出的序列\n",
    "        item_data = []\n",
    "        for i in range(len(item_list)):\n",
    "            index = index_list[i][0]\n",
    "            item = Bin('0', item_list[index][1], item_list[index][2], item_list[index][3], item_list[index][4], 0, item_list[index][5]) #货物的0长1宽2高和3价值\n",
    "            item_data.append(item)\n",
    "        return item_data\n",
    "\n",
    "    def bin_create_num(self, item_list, index_list): # item 为array,为得出的序列\n",
    "        # 装载成为物品的顺序 num表示有货物初始编号\n",
    "        item_data = []\n",
    "        print(len(item_list))\n",
    "        for i in range(len(item_list)):\n",
    "            # print(i)\n",
    "            index = index_list[i][0]\n",
    "            item = Bin(item_list[index][0], item_list[index][1], item_list[index][2], item_list[index][3], item_list[index][4], 0, item_list[index][5]) #货物的0长1宽2高和3价值\n",
    "            item_data.append(item)\n",
    "        return item_data\n",
    "\n",
    "    def car_list_create(self):\n",
    "        car2 = Car('3DBin_20', 20, 20, 20, 3000, Attr.Position, 0)\n",
    "        car0 = Car('3DBin_12', 12, 12, 12, 3000, Attr.Position, 0)\n",
    "        car1 = Car('3DBin_16', 16, 16, 16, 3000, Attr.Position, 0)\n",
    "        #car3 = Car('3DBin_24', 24, 24, 24, 3000, Attr.Position, 0)\n",
    "        #car3 = Car('3DBin_30', 30, 30, 30, 3000, Attr.Position, 0)\n",
    "        car3 = Car('3DBin_actual', 42, 16, 18, 3000, Attr.Position, 0) #长度单位：公分；3000表示载重3000千克\n",
    "\n",
    "        # car4 = Car('3t货车', 25, 25, 25, 3000, Attr.Position, 0)\n",
    "        # car5 = Car('3t货车', 26, 26, 26, 3000, Attr.Position, 0)\n",
    "        # car6 = Car('3t货车', 27, 27, 27, 3000, Attr.Position, 0)\n",
    "        # car7 = Car('3t货车', 28, 28, 28, 3000, Attr.Position, 0)\n",
    "        # car8 = Car('3t货车', 29, 29, 29, 3000, Attr.Position, 0)\n",
    "        # car9 = Car('3t货车', 30, 30, 30, 3000, Attr.Position, 0)\n",
    "        # car10 = Car('3t货车', 31, 31, 31, 3000, Attr.Position, 0)\n",
    "        # car11 = Car('3t货车', 32, 32, 32, 3000, Attr.Position, 0)\n",
    "        car_list20 = [car2]\n",
    "        car_list12 = [car0]\n",
    "        car_list16 = [car1]\n",
    "        car_list24 = [car3]\n",
    "        return car_list12,car_list16,car_list20,car_list24\n",
    "        # car_list25 = [car4]\n",
    "        # car_list26 = [car5]\n",
    "        # car_list27 = [car6]\n",
    "        # car_list28 = [car7]\n",
    "        # carlist29 = [car8]\n",
    "        # car_list3_0 = [car9]\n",
    "        # car_list31 = [car10]\n",
    "        # car_list32 = [car11]\n",
    "    #************************  此方法需要重写 ******************\n",
    "    # 功能设想：将多种启发式算法的结果作为列表输入，可以是所有大小类型的货箱的实验结果，得到每种货箱的均值或者中位数作为baseline\n",
    "    def baseline_b(self,np_num):\n",
    "        sum = 0\n",
    "        for i in np_num:\n",
    "            sum += i\n",
    "        return float(format(sum / len(np_num), '.7f'))\n",
    "    # b_12 = 0.6  # baseline_b(bin_12_16_20_24[0,:])\n",
    "    # b_16 = 0.6  # baseline_b(bin_12_16_20_24[1,:])\n",
    "    # b_20 = 0.6  # baseline_b(bin_12_16_20_24[2,:])\n",
    "    # b_24 = 0.6  # baseline_b(bin_12_16_20_24[3,:])\n",
    "\n",
    "\n",
    "# 生成100个2-4之间的随机长宽高组成的数据\n",
    "\n",
    "#货箱属性类\n",
    "# Car: name,length,width,height,Max_weight,position,temp_volume\n",
    "class Car:\n",
    "    def __init__(self,name,length,width,height,Max_weight,position,temp_volume):\n",
    "        self.name = name\n",
    "        self.length = length\n",
    "        self.width = width\n",
    "        self.height = height\n",
    "        # self.weight = weight\n",
    "        self.volume = length * width * height\n",
    "        self.Max_weight = Max_weight\n",
    "        self.position = position\n",
    "        self.temp_volume = temp_volume\n",
    "#货物属性类\n",
    "# Bin: name,length,width,height,weight,pose\n",
    "class Bin:\n",
    "    def __init__(self,name,length,width,height,weight,pose,family):\n",
    "        self.name = name\n",
    "        self.length = length\n",
    "        self.width = width\n",
    "        self.height = height\n",
    "        self.weight = weight\n",
    "        self.volume = length * width * height\n",
    "        self.pose = pose\n",
    "        self.x = 0\n",
    "        self.y = 0\n",
    "        self.z = 0\n",
    "        self.family = family #货物族名\n",
    "    def get_dimension(self):\n",
    "        if self.pose == Attr.Pose_wh_front:\n",
    "            d = [self.length, self.width, self.height]  # 就是正面\n",
    "        elif self.pose == Attr.Pose_hw_front:\n",
    "            d = [self.length, self.height, self.width]\n",
    "        elif self.pose == Attr.Pose_dh_front:\n",
    "            d = [self.width, self.length, self.height]\n",
    "        elif self.pose == Attr.Pose_hd_front:\n",
    "            d = [self.width, self.height, self.length]\n",
    "        elif self.pose == Attr.Pose_wd_front:\n",
    "            d = [self.height, self.length, self.width]\n",
    "        elif self.pose == Attr.Pose_dw_front:\n",
    "            d = [self.height, self.width, self.length]\n",
    "        else:\n",
    "            d = []\n",
    "        return d\n",
    "#货物朝向类\n",
    "class Attr:\n",
    "    # 三个坐标轴\n",
    "    Axis_x = 0\n",
    "    Axis_y = 1\n",
    "    Axis_z = 2\n",
    "    # 原点坐标\n",
    "    Position = [0, 0, 0]\n",
    "    # 六个朝向\n",
    "    Pose_wh_front = 0\n",
    "    Pose_hw_front = 1\n",
    "    Pose_hd_front = 2\n",
    "    Pose_dh_front = 3\n",
    "    Pose_wd_front = 4\n",
    "    Pose_dw_front = 5\n",
    "\n",
    "# 装载方式变换\n",
    "class PackingMethod:\n",
    "    def first_put(self):\n",
    "        arg = 0\n",
    "        for i in range(6):\n",
    "            d = PackingMethod.set_pose(self, i).get_dimension()\n",
    "            if d[Attr.Axis_x] > d[Attr.Axis_y] and d[Attr.Axis_y] > d[Attr.Axis_z]:\n",
    "                arg = i\n",
    "            else:\n",
    "                pass\n",
    "        return arg\n",
    "    # 找到长是最长的 ，宽次要 高最短的那种形状填进去\n",
    "    # 适合放进去的方式\n",
    "    def pose_fit_arg(self, temp_L, temp_W, temp_H):  # 当前对象\n",
    "        arg_ = 10000\n",
    "        dv_ = 10000\n",
    "        for i in range(6):\n",
    "            d = PackingMethod.set_pose(self, i).get_dimension()\n",
    "            #*******************************************          欧氏距离       ****************************************\n",
    "            sr = math.sqrt((temp_L - d[Attr.Axis_x]) ** 2 + (temp_W - d[Attr.Axis_y]) ** 2 + (\n",
    "                        temp_H - d[Attr.Axis_z]) ** 2)  # 我选择的判定方式是平方根\n",
    "            if dv_ > sr:\n",
    "                dv_ = sr\n",
    "                arg_ = i\n",
    "            else:\n",
    "                continue\n",
    "        return arg_\n",
    "        # 返回的是 fanhuideshi\n",
    "\n",
    "    def set_pose(self, num):\n",
    "        if num >= 0 and num <= 5:\n",
    "            self.pose = num\n",
    "            return self\n",
    "        else:\n",
    "            print('数据异常')\n",
    "\n",
    "\n",
    "class Sort:\n",
    "    # 排序  车已经从大到小排序完了，这里车辆并没有付费，考虑到成本的话，需要加一下\n",
    "    def Sort_Car(Car_list):\n",
    "        Car_list.sort(key=lambda x: x.volume, reverse=True)\n",
    "        return Car_list\n",
    "    #货物按照体积大小排序，从小到大\n",
    "    def Sort_Box(Car_list):\n",
    "        Car_list.sort(key=lambda x: x.volume, reverse=False)\n",
    "        return Car_list\n",
    "\n",
    "class Packer:\n",
    "\n",
    "    def packer_own(self,car_list_sort_fixed, box_sort_list):\n",
    "        temp_l = 0    # 临时长，这是装入每个箱子的参照\n",
    "        temp_w = 0\n",
    "        temp_h = 0\n",
    "        temp_ll = 0\n",
    "        temp_ww = 0\n",
    "        temp_hh = 0\n",
    "        c = 0\n",
    "        i_volume = 0\n",
    "        b_volume = 0\n",
    "        item_list = []\n",
    "        peritem_list = []\n",
    "        #for循环所有货箱 i:Car类\n",
    "        for i in car_list_sort_fixed:\n",
    "            # box_list_width = []\n",
    "            # box_list_height = []\n",
    "            while(True):\n",
    "                # print(i.position)\n",
    "                if i.position == [0,0,0]:  # 这里绝对不是i.position\n",
    "                    if ((box_sort_list[0].height > i.height) or ( \\\n",
    "                                    box_sort_list[0].width > i.width) or ( \\\n",
    "                                    box_sort_list[0].length > i.length)):\n",
    "                        break\n",
    "                        # 这个意思就是说如果最开始就放不进去就不要放了\n",
    "                    elif (\n",
    "                            (box_sort_list[0].height <= i.height) and (\\\n",
    "                                    box_sort_list[0].width <= i.width) and (\\\n",
    "                                            box_sort_list[0].length <= i.length)\n",
    "                    ):\n",
    "                        d = PackingMethod.set_pose(box_sort_list[0],\n",
    "                                                   PackingMethod.first_put(box_sort_list[0])).get_dimension()\n",
    "                        temp_l = d[Attr.Axis_x]  # 初代放置作为模型开始对比\n",
    "                        temp_w = d[Attr.Axis_y]\n",
    "                        temp_h = d[Attr.Axis_z]\n",
    "                        temp_ll = temp_l\n",
    "                        temp_ww = temp_w\n",
    "                        temp_hh = temp_h\n",
    "                        i.position[0] += d[Attr.Axis_x]    # 这里暂时相加\n",
    "                        # i.position[attr.Attr.Axis_y]+=d[attr.Attr.Axis_y]\n",
    "                        # i.position[attr.Attr.Axis_z]+=d[attr.Attr.Axis_z]\n",
    "                        item_list.append(box_sort_list[0])\n",
    "                        peritem_list.append(box_sort_list[0].volume/i.volume)\n",
    "                        i_volume += box_sort_list[0].volume\n",
    "                        # box_list_width.append(box_sort_list[0].width)\n",
    "                        # box_list_height.append(box_sort_list[0].height)\n",
    "                        c += 1\n",
    "                        box_sort_list.pop(0)\n",
    "                        continue\n",
    "                    # 以上是空车第一次放置的时候\n",
    "                else:\n",
    "                    for j in box_sort_list[0:]: #原[1:]\n",
    "                        temp_box = PackingMethod.set_pose(j, PackingMethod.pose_fit_arg(j, temp_l, temp_w,temp_h)).get_dimension()\n",
    "                        if (temp_box[Attr.Axis_z] > (i.height - i.position[Attr.Axis_z])) or (temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]) and \\\n",
    "                                    temp_box[Attr.Axis_z] >(i.width-i.position[2]-temp_hh)):\n",
    "                            temp_l = 0\n",
    "                            temp_w = 0\n",
    "                            temp_h = 0\n",
    "                            temp_ll = 0\n",
    "                            temp_hh = 0\n",
    "                            temp_ww = 0\n",
    "                            i.position[Attr.Axis_x] = 0\n",
    "                            i.position[Attr.Axis_y] = 0\n",
    "                            i.position[Attr.Axis_z] = 0\n",
    "                            break\n",
    "                            # 这句话意思即是后来的箱子已经放不进去了，换句话说就是已经装满了,然后变量全部归零\n",
    "                        else:\n",
    "                            if temp_box[Attr.Axis_y] <= (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= ( i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                i.position[0] += temp_box[Attr.Axis_x]\n",
    "                                # print(\"7\")\n",
    "                                # global temp_ll, temp_ww, temp_hh\n",
    "                                temp_ll = max(temp_ll, temp_box[Attr.Axis_x])  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = max(temp_ww, temp_box[Attr.Axis_y])\n",
    "                                temp_hh = max(temp_hh, temp_box[Attr.Axis_z])\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume/i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                Done = False\n",
    "                                continue\n",
    "                                # 都小于的时候，每一个块的长度相加 ，宽度 ，高度进行记录\n",
    "                            elif temp_box[Attr.Axis_y] <= (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] > (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                i.position[0] = 0\n",
    "                                i.position[0] += temp_box[Attr.Axis_x]\n",
    "                                i.position[1] += temp_ww\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = max(temp_hh, temp_box[Attr.Axis_z])\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume/i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                                # 换到下一行，x重新置为0，将当前的最大W加上\n",
    "\n",
    "                            elif temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <=(i.width-i.position[2]-temp_hh):\n",
    "                                i.position[2] += temp_hh\n",
    "                                i.position[0] = temp_box[Attr.Axis_x]\n",
    "                                i.position[1] = temp_box[Attr.Axis_y]\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = temp_box[Attr.Axis_z]\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume/i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                            elif temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] > (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                i.position[2] += temp_hh\n",
    "                                i.position[0] = temp_box[Attr.Axis_x]\n",
    "                                i.position[1] = temp_box[Attr.Axis_y]\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = temp_box[Attr.Axis_z]\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume/i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                break\n",
    "            b_volume = i.volume\n",
    "        return i_volume / b_volume , c , item_list, peritem_list\n",
    "\n",
    "    def packer_oneCar_cube(self, car_list_sort_fixed, box_sort_list):\n",
    "        temp_l = 0  # 临时长，这是装入每个箱子的参照\n",
    "        temp_w = 0\n",
    "        temp_h = 0\n",
    "        temp_ll = 0\n",
    "        temp_ww = 0\n",
    "        temp_hh = 0\n",
    "        c = 0\n",
    "        i_volume = 0\n",
    "        b_volume = 0\n",
    "        item_list = []\n",
    "        peritem_list = []\n",
    "        cubes = []\n",
    "        x_ = car_list_sort_fixed[0].length\n",
    "        y_ = car_list_sort_fixed[0].width\n",
    "        z_ = car_list_sort_fixed[0].height\n",
    "\n",
    "        x, y, z = np.indices((x_, y_, z_)) #x,y,z type:ndarry\n",
    "\n",
    "        # for循环所有货箱 i:Car类\n",
    "        for i in car_list_sort_fixed:\n",
    "            # box_list_width = []\n",
    "            # box_list_height = []\n",
    "            while (True):\n",
    "                # print(i.position)\n",
    "                if i.position == [0, 0, 0]:  # 这里绝对不是i.position\n",
    "                    if ((box_sort_list[0].height > i.height) or ( \\\n",
    "                                    box_sort_list[0].width > i.width) or ( \\\n",
    "                                    box_sort_list[0].length > i.length)):\n",
    "                        break\n",
    "                        # 这个意思就是说如果最开始就放不进去就不要放了\n",
    "                    elif (\n",
    "                            (box_sort_list[0].height <= i.height) and ( \\\n",
    "                                    box_sort_list[0].width <= i.width) and ( \\\n",
    "                                            box_sort_list[0].length <= i.length)\n",
    "                    ):\n",
    "                        cube = (x <= box_sort_list[0].length.item()-1) & \\\n",
    "                               (y <= box_sort_list[0].width.item()-1) & \\\n",
    "                               (z <= box_sort_list[0].height.item()-1)\n",
    "                        cubes.append(cube)\n",
    "                        d = PackingMethod.set_pose(box_sort_list[0],\n",
    "                                                   PackingMethod.first_put(box_sort_list[0])).get_dimension()\n",
    "                        temp_l = d[Attr.Axis_x]  # 初代放置作为模型开始对比\n",
    "                        temp_w = d[Attr.Axis_y]\n",
    "                        temp_h = d[Attr.Axis_z]\n",
    "                        temp_ll = temp_l\n",
    "                        temp_ww = temp_w\n",
    "                        temp_hh = temp_h\n",
    "                        i.position[0] += d[Attr.Axis_x]  # 这里暂时相加\n",
    "                        # i.position[attr.Attr.Axis_y]+=d[attr.Attr.Axis_y]\n",
    "                        # i.position[attr.Attr.Axis_z]+=d[attr.Attr.Axis_z]\n",
    "                        item_list.append(box_sort_list[0])\n",
    "                        peritem_list.append(box_sort_list[0].volume / i.volume)\n",
    "                        i_volume += box_sort_list[0].volume\n",
    "                        # box_list_width.append(box_sort_list[0].width)\n",
    "                        # box_list_height.append(box_sort_list[0].height)\n",
    "                        c += 1\n",
    "                        box_sort_list.pop(0)\n",
    "                        continue\n",
    "                    # 以上是空车第一次放置的时候\n",
    "                else:\n",
    "                    for j in box_sort_list[0:]:  # 原[1:]\n",
    "                        temp_box = PackingMethod.set_pose(j, PackingMethod.pose_fit_arg(j, temp_l, temp_w,\n",
    "                                                                                        temp_h)).get_dimension()\n",
    "                        if (temp_box[Attr.Axis_z] > (i.height - i.position[Attr.Axis_z])) or (\n",
    "                                temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                temp_box[Attr.Axis_z] <= (i.height - i.position[2]) and \\\n",
    "                                temp_box[Attr.Axis_z] > (i.width - i.position[2] - temp_hh)):\n",
    "                            temp_l = 0\n",
    "                            temp_w = 0\n",
    "                            temp_h = 0\n",
    "                            temp_ll = 0\n",
    "                            temp_hh = 0\n",
    "                            temp_ww = 0\n",
    "                            i.position[Attr.Axis_x] = 0\n",
    "                            i.position[Attr.Axis_y] = 0\n",
    "                            i.position[Attr.Axis_z] = 0\n",
    "                            break\n",
    "                            # 这句话意思即是后来的箱子已经放不进去了，换句话说就是已经装满了,然后变量全部归零\n",
    "                        else:\n",
    "                            if temp_box[Attr.Axis_y] <= (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                #放置货物在货箱中的位置\n",
    "                                c = i.position[0]\n",
    "                                #print(type(x), int(i.position[0] + temp_box[Attr.Axis_x].item()))\n",
    "\n",
    "                                cube = (x <= int(i.position[0] + temp_box[Attr.Axis_x].item())-1) & (x >= int(i.position[0])) &\\\n",
    "                                    (y <= int(i.position[1] + temp_box[Attr.Axis_y].item())-1) & (y >= int(i.position[1])) &\\\n",
    "                                           (z <= int(i.position[2] + temp_box[Attr.Axis_z].item())-1) & (z >= int(i.position[2]))\n",
    "                                cubes.append(cube)\n",
    "\n",
    "                                #更新装箱层数\n",
    "                                i.position[0] += temp_box[Attr.Axis_x]\n",
    "                                # print(\"7\")\n",
    "                                # global temp_ll, temp_ww, temp_hh\n",
    "                                temp_ll = max(temp_ll, temp_box[Attr.Axis_x])  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = max(temp_ww, temp_box[Attr.Axis_y])\n",
    "                                temp_hh = max(temp_hh, temp_box[Attr.Axis_z])\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume / i.volume)\n",
    "                                i_volume += j.volume\n",
    "\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                Done = False\n",
    "                                continue\n",
    "                                # 都小于的时候，每一个块的长度相加 ，宽度 ，高度进行记录\n",
    "                            elif temp_box[Attr.Axis_y] <= (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] > (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "\n",
    "                                #更新装箱层数\n",
    "                                i.position[0] = 0\n",
    "                                i.position[0] += temp_box[Attr.Axis_x]\n",
    "                                i.position[1] += temp_ww\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = max(temp_hh, temp_box[Attr.Axis_z])\n",
    "                                #放置货物在货箱中的位置\n",
    "                                cube = (x >= int(i.position[0] - temp_box[Attr.Axis_x].item())) & (x <= int(i.position[0])-1) & \\\n",
    "                                    (y <= int(i.position[1] + temp_box[Attr.Axis_y].item())-1) & (y >= int(i.position[1])) & \\\n",
    "                                           (z <= int(i.position[2] + temp_box[Attr.Axis_z].item())-1) & (z >= int(i.position[2]))\n",
    "                                cubes.append(cube)\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume / i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                                # 换到下一行，x重新置为0，将当前的最大W加上\n",
    "\n",
    "                            elif temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.width - i.position[2] - temp_hh):\n",
    "                                #更新装箱层数\n",
    "                                i.position[2] += temp_hh\n",
    "                                i.position[0] = temp_box[Attr.Axis_x]\n",
    "                                i.position[1] = 0#temp_box[Attr.Axis_y]  ####\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = temp_box[Attr.Axis_z]\n",
    "                                #放置货物在货箱中的位置\n",
    "\n",
    "                                cube = (x >= int(i.position[0] - temp_box[Attr.Axis_x].item())) & (x <= int(i.position[0])-1) & \\\n",
    "                                    (y <= int(i.position[1] + temp_box[Attr.Axis_y].item())-1) & (y >= int(i.position[1])) & \\\n",
    "                                           (z >= int(i.position[2] - temp_box[Attr.Axis_z])) & (z <= int(i.position[2])-1)\n",
    "                                cubes.append(cube)\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume / i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                            elif temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] > (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                #更新装箱层数\n",
    "                                i.position[2] += temp_hh\n",
    "                                i.position[0] = temp_box[Attr.Axis_x]\n",
    "                                i.position[1] = 0#temp_box[Attr.Axis_y]#####\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = temp_box[Attr.Axis_z]\n",
    "                                #放置货物在货箱中的位置\n",
    "                                cube = (x >= int(i.position[0] - temp_box[Attr.Axis_x].item())) & (x <= int(i.position[0])-1 ) & \\\n",
    "                                    (y <= int(i.position[1] + temp_box[Attr.Axis_y].item())-1) & (y >= int(i.position[1])) & \\\n",
    "                                           (z >= int(i.position[2] - temp_box[Attr.Axis_z].item())) & (z <= int(i.position[2])-1)\n",
    "                                cubes.append(cube)\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume / i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "\n",
    "                break\n",
    "            b_volume = i.volume\n",
    "        return i_volume / b_volume, c, item_list, cubes\n",
    "    def packer_oneCar_cube_family(self, car_list_sort_fixed, box_sort_list):\n",
    "        temp_l = 0  # 临时长，这是装入每个箱子的参照\n",
    "        temp_w = 0\n",
    "        temp_h = 0\n",
    "        temp_ll = 0\n",
    "        temp_ww = 0\n",
    "        temp_hh = 0\n",
    "        c = 0\n",
    "        i_volume = 0\n",
    "        b_volume = 0\n",
    "        item_list = []\n",
    "        peritem_list = []\n",
    "        cubes = []\n",
    "        family = []  #family\n",
    "        x_ = car_list_sort_fixed[0].length\n",
    "        y_ = car_list_sort_fixed[0].width\n",
    "        z_ = car_list_sort_fixed[0].height\n",
    "\n",
    "        x, y, z = np.indices((x_, y_, z_)) #x,y,z type:ndarry\n",
    "\n",
    "        # for循环所有货箱 i:Car类\n",
    "        for i in car_list_sort_fixed:\n",
    "            # box_list_width = []\n",
    "            # box_list_height = []\n",
    "            while (True):\n",
    "                # print(i.position)\n",
    "                if i.position == [0, 0, 0]:  # 这里绝对不是i.position\n",
    "                    if ((box_sort_list[0].height > i.height) or ( \\\n",
    "                                    box_sort_list[0].width > i.width) or ( \\\n",
    "                                    box_sort_list[0].length > i.length)):\n",
    "                        break\n",
    "                        # 这个意思就是说如果最开始就放不进去就不要放了\n",
    "                    elif (\n",
    "                            (box_sort_list[0].height <= i.height) and ( \\\n",
    "                                    box_sort_list[0].width <= i.width) and ( \\\n",
    "                                            box_sort_list[0].length <= i.length)\n",
    "                    ):\n",
    "                        cube = (x <= box_sort_list[0].length.item()-1) & \\\n",
    "                               (y <= box_sort_list[0].width.item()-1) & \\\n",
    "                               (z <= box_sort_list[0].height.item()-1)\n",
    "                        cubes.append(cube)\n",
    "                        d = PackingMethod.set_pose(box_sort_list[0],\n",
    "                                                   PackingMethod.first_put(box_sort_list[0])).get_dimension()\n",
    "                        temp_l = d[Attr.Axis_x]  # 初代放置作为模型开始对比\n",
    "                        temp_w = d[Attr.Axis_y]\n",
    "                        temp_h = d[Attr.Axis_z]\n",
    "                        temp_ll = temp_l\n",
    "                        temp_ww = temp_w\n",
    "                        temp_hh = temp_h\n",
    "                        i.position[0] += d[Attr.Axis_x]  # 这里暂时相加\n",
    "                        # i.position[attr.Attr.Axis_y]+=d[attr.Attr.Axis_y]\n",
    "                        # i.position[attr.Attr.Axis_z]+=d[attr.Attr.Axis_z]\n",
    "                        item_list.append(box_sort_list[0])\n",
    "                        family.append(box_sort_list[0].family)\n",
    "\n",
    "                        peritem_list.append(box_sort_list[0].volume / i.volume)\n",
    "                        i_volume += box_sort_list[0].volume\n",
    "                        # box_list_width.append(box_sort_list[0].width)\n",
    "                        # box_list_height.append(box_sort_list[0].height)\n",
    "                        c += 1\n",
    "                        box_sort_list.pop(0)\n",
    "                        continue\n",
    "                    # 以上是空车第一次放置的时候\n",
    "                else:\n",
    "                    for j in box_sort_list[0:]:  # 原[1:]\n",
    "                        temp_box = PackingMethod.set_pose(j, PackingMethod.pose_fit_arg(j, temp_l, temp_w,\n",
    "                                                                                        temp_h)).get_dimension()\n",
    "                        if (temp_box[Attr.Axis_z] > (i.height - i.position[Attr.Axis_z])) or (\n",
    "                                temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                temp_box[Attr.Axis_z] <= (i.height - i.position[2]) and \\\n",
    "                                temp_box[Attr.Axis_z] > (i.width - i.position[2] - temp_hh)):\n",
    "                            temp_l = 0\n",
    "                            temp_w = 0\n",
    "                            temp_h = 0\n",
    "                            temp_ll = 0\n",
    "                            temp_hh = 0\n",
    "                            temp_ww = 0\n",
    "                            i.position[Attr.Axis_x] = 0\n",
    "                            i.position[Attr.Axis_y] = 0\n",
    "                            i.position[Attr.Axis_z] = 0\n",
    "                            break\n",
    "                            # 这句话意思即是后来的箱子已经放不进去了，换句话说就是已经装满了,然后变量全部归零\n",
    "                        else:\n",
    "                            if temp_box[Attr.Axis_y] <= (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                #放置货物在货箱中的位置\n",
    "                                c = i.position[0]\n",
    "                                #print(type(x), int(i.position[0] + temp_box[Attr.Axis_x].item()))\n",
    "\n",
    "                                cube = (x <= int(i.position[0] + temp_box[Attr.Axis_x].item())-1) & (x >= int(i.position[0])) &\\\n",
    "                                    (y <= int(i.position[1] + temp_box[Attr.Axis_y].item())-1) & (y >= int(i.position[1])) &\\\n",
    "                                           (z <= int(i.position[2] + temp_box[Attr.Axis_z].item())-1) & (z >= int(i.position[2]))\n",
    "                                cubes.append(cube)\n",
    "\n",
    "                                #更新装箱层数\n",
    "                                i.position[0] += temp_box[Attr.Axis_x]\n",
    "                                # print(\"7\")\n",
    "                                # global temp_ll, temp_ww, temp_hh\n",
    "                                temp_ll = max(temp_ll, temp_box[Attr.Axis_x])  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = max(temp_ww, temp_box[Attr.Axis_y])\n",
    "                                temp_hh = max(temp_hh, temp_box[Attr.Axis_z])\n",
    "                                item_list.append(j)\n",
    "                                family.append(box_sort_list[0].family)\n",
    "                                peritem_list.append(j.volume / i.volume)\n",
    "                                i_volume += j.volume\n",
    "\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                Done = False\n",
    "                                continue\n",
    "                                # 都小于的时候，每一个块的长度相加 ，宽度 ，高度进行记录\n",
    "                            elif temp_box[Attr.Axis_y] <= (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] > (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "\n",
    "                                #更新装箱层数\n",
    "                                i.position[0] = 0\n",
    "                                i.position[0] += temp_box[Attr.Axis_x]\n",
    "                                i.position[1] += temp_ww\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = max(temp_hh, temp_box[Attr.Axis_z])\n",
    "                                #放置货物在货箱中的位置\n",
    "                                cube = (x >= int(i.position[0] - temp_box[Attr.Axis_x].item())) & (x <= int(i.position[0])-1) & \\\n",
    "                                    (y <= int(i.position[1] + temp_box[Attr.Axis_y].item())-1) & (y >= int(i.position[1])) & \\\n",
    "                                           (z <= int(i.position[2] + temp_box[Attr.Axis_z].item())-1) & (z >= int(i.position[2]))\n",
    "                                cubes.append(cube)\n",
    "                                item_list.append(j)\n",
    "                                family.append(box_sort_list[0].family)\n",
    "                                peritem_list.append(j.volume / i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                                # 换到下一行，x重新置为0，将当前的最大W加上\n",
    "\n",
    "                            elif temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.width - i.position[2] - temp_hh):\n",
    "                                #更新装箱层数\n",
    "                                i.position[2] += temp_hh\n",
    "                                i.position[0] = temp_box[Attr.Axis_x]\n",
    "                                i.position[1] = 0#temp_box[Attr.Axis_y]  ####\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = temp_box[Attr.Axis_z]\n",
    "                                #放置货物在货箱中的位置\n",
    "\n",
    "                                cube = (x >= int(i.position[0] - temp_box[Attr.Axis_x].item())) & (x <= int(i.position[0])-1) & \\\n",
    "                                    (y <= int(i.position[1] + temp_box[Attr.Axis_y].item())-1) & (y >= int(i.position[1])) & \\\n",
    "                                           (z >= int(i.position[2] - temp_box[Attr.Axis_z])) & (z <= int(i.position[2])-1)\n",
    "                                cubes.append(cube)\n",
    "                                item_list.append(j)\n",
    "                                family.append(box_sort_list[0].family)\n",
    "                                peritem_list.append(j.volume / i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                            elif temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] > (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                #更新装箱层数\n",
    "                                i.position[2] += temp_hh\n",
    "                                i.position[0] = temp_box[Attr.Axis_x]\n",
    "                                i.position[1] = 0#temp_box[Attr.Axis_y]#####\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = temp_box[Attr.Axis_z]\n",
    "                                #放置货物在货箱中的位置\n",
    "                                cube = (x >= int(i.position[0] - temp_box[Attr.Axis_x].item())) & (x <= int(i.position[0])-1 ) & \\\n",
    "                                    (y <= int(i.position[1] + temp_box[Attr.Axis_y].item())-1) & (y >= int(i.position[1])) & \\\n",
    "                                           (z >= int(i.position[2] - temp_box[Attr.Axis_z].item())) & (z <= int(i.position[2])-1)\n",
    "                                cubes.append(cube)\n",
    "                                item_list.append(j)\n",
    "                                family.append(box_sort_list[0].family)\n",
    "                                peritem_list.append(j.volume / i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "\n",
    "                break\n",
    "            b_volume = i.volume\n",
    "        return i_volume / b_volume, c, item_list, cubes, family\n",
    "\n",
    "    def packer_family(self, car_list_sort_fixed, box_sort_list):\n",
    "        temp_l = 0    # 临时长，这是装入每个箱子的参照\n",
    "        temp_w = 0\n",
    "        temp_h = 0\n",
    "        temp_ll = 0\n",
    "        temp_ww = 0\n",
    "        temp_hh = 0\n",
    "        c = 0\n",
    "        i_volume = 0\n",
    "        b_volume = 0\n",
    "        item_list = []\n",
    "        peritem_list = []\n",
    "        family_list = []\n",
    "        #for循环所有货箱 i:Car类\n",
    "        for i in car_list_sort_fixed:\n",
    "            # box_list_width = []\n",
    "            # box_list_height = []\n",
    "            while(True):\n",
    "                # print(i.position)\n",
    "                if i.position == [0,0,0]:  # 这里绝对不是i.position\n",
    "                    if ((box_sort_list[0].height > i.height) or ( \\\n",
    "                                    box_sort_list[0].width > i.width) or ( \\\n",
    "                                    box_sort_list[0].length > i.length)):\n",
    "                        break\n",
    "                        # 这个意思就是说如果最开始就放不进去就不要放了\n",
    "                    elif (\n",
    "                            (box_sort_list[0].height <= i.height) and (\\\n",
    "                                    box_sort_list[0].width <= i.width) and (\\\n",
    "                                            box_sort_list[0].length <= i.length)\n",
    "                    ):\n",
    "                        d = PackingMethod.set_pose(box_sort_list[0],\n",
    "                                                   PackingMethod.first_put(box_sort_list[0])).get_dimension()\n",
    "                        temp_l = d[Attr.Axis_x]  # 初代放置作为模型开始对比\n",
    "                        temp_w = d[Attr.Axis_y]\n",
    "                        temp_h = d[Attr.Axis_z]\n",
    "                        temp_ll = temp_l\n",
    "                        temp_ww = temp_w\n",
    "                        temp_hh = temp_h\n",
    "                        i.position[0] += d[Attr.Axis_x]    # 这里暂时相加\n",
    "                        # i.position[attr.Attr.Axis_y]+=d[attr.Attr.Axis_y]\n",
    "                        # i.position[attr.Attr.Axis_z]+=d[attr.Attr.Axis_z]\n",
    "                        item_list.append(box_sort_list[0])\n",
    "                        peritem_list.append(box_sort_list[0].volume/i.volume)\n",
    "                        i_volume += box_sort_list[0].volume\n",
    "                        # box_list_width.append(box_sort_list[0].width)\n",
    "                        # box_list_height.append(box_sort_list[0].height)\n",
    "                        c += 1\n",
    "                        family_list.append(box_sort_list[0].family)\n",
    "                        box_sort_list.pop(0)\n",
    "                        continue\n",
    "                    # 以上是空车第一次放置的时候\n",
    "                else:\n",
    "                    for j in box_sort_list[0:]: #原[1:]\n",
    "                        temp_box = PackingMethod.set_pose(j, PackingMethod.pose_fit_arg(j, temp_l, temp_w,temp_h)).get_dimension()\n",
    "                        if (temp_box[Attr.Axis_z] > (i.height - i.position[Attr.Axis_z])) or (temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]) and \\\n",
    "                                    temp_box[Attr.Axis_z] >(i.width-i.position[2]-temp_hh)):\n",
    "                            temp_l = 0\n",
    "                            temp_w = 0\n",
    "                            temp_h = 0\n",
    "                            temp_ll = 0\n",
    "                            temp_hh = 0\n",
    "                            temp_ww = 0\n",
    "                            i.position[Attr.Axis_x] = 0\n",
    "                            i.position[Attr.Axis_y] = 0\n",
    "                            i.position[Attr.Axis_z] = 0\n",
    "                            break\n",
    "                            # 这句话意思即是后来的箱子已经放不进去了，换句话说就是已经装满了,然后变量全部归零\n",
    "                        else:\n",
    "                            if temp_box[Attr.Axis_y] <= (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= ( i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                i.position[0] += temp_box[Attr.Axis_x]\n",
    "                                # print(\"7\")\n",
    "                                # global temp_ll, temp_ww, temp_hh\n",
    "                                temp_ll = max(temp_ll, temp_box[Attr.Axis_x])  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = max(temp_ww, temp_box[Attr.Axis_y])\n",
    "                                temp_hh = max(temp_hh, temp_box[Attr.Axis_z])\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume/i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                family_list.append(box_sort_list[0].family)\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                Done = False\n",
    "                                continue\n",
    "                                # 都小于的时候，每一个块的长度相加 ，宽度 ，高度进行记录\n",
    "                            elif temp_box[Attr.Axis_y] <= (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] > (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                i.position[0] = 0\n",
    "                                i.position[0] += temp_box[Attr.Axis_x]\n",
    "                                i.position[1] += temp_ww\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = max(temp_hh, temp_box[Attr.Axis_z])\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume/i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                family_list.append(box_sort_list[0].family)\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                                # 换到下一行，x重新置为0，将当前的最大W加上\n",
    "\n",
    "                            elif temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] <= (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <=(i.width-i.position[2]-temp_hh):\n",
    "                                i.position[2] += temp_hh\n",
    "                                i.position[0] = temp_box[Attr.Axis_x]\n",
    "                                i.position[1] = temp_box[Attr.Axis_y]\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = temp_box[Attr.Axis_z]\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume/i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                family_list.append(box_sort_list[0].family)\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                            elif temp_box[Attr.Axis_y] > (i.width - i.position[1]) and \\\n",
    "                                    temp_box[Attr.Axis_x] > (i.length - i.position[0]) and \\\n",
    "                                    temp_box[Attr.Axis_z] <= (i.height - i.position[2]):\n",
    "                                i.position[2] += temp_hh\n",
    "                                i.position[0] = temp_box[Attr.Axis_x]\n",
    "                                i.position[1] = temp_box[Attr.Axis_y]\n",
    "                                temp_ll = temp_box[Attr.Axis_x]  # 返回当前装箱体与之前装箱体的长宽高数据，并保留当前的最大的数值\n",
    "                                temp_ww = temp_box[Attr.Axis_y]\n",
    "                                temp_hh = temp_box[Attr.Axis_z]\n",
    "                                item_list.append(j)\n",
    "                                peritem_list.append(j.volume/i.volume)\n",
    "                                i_volume += j.volume\n",
    "                                family_list.append(box_sort_list[0].family)\n",
    "                                box_sort_list.pop(0)\n",
    "                                c += 1\n",
    "                                continue\n",
    "                break\n",
    "            b_volume = i.volume\n",
    "        return i_volume / b_volume, c, item_list, family_list\n",
    "class PackerStable:\n",
    "    #稳定装载，货物摆放稳定，没有悬空\n",
    "\n",
    "    def judge_position(self):\n",
    "        #判断能否装载进货箱\n",
    "\n",
    "        return 0 #位置\n",
    "    def init_height_graph(self, Car):\n",
    "        #self.height_graph = torch.zeros((Car.length, Car.width))\n",
    "        self.height_graph = torch.zeros((Car.length, Car.width))\n",
    "\n",
    "    def update_height_graph(self, item, state):\n",
    "        # 根据货物属性和当前高度图更新高度图\n",
    "        new_state = state.clone()\n",
    "        le = int(item.x)\n",
    "        ri = int(item.x+item.length)\n",
    "        up = int(item.y)\n",
    "        do = int(item.y+item.width)\n",
    "        max_h = torch.max(new_state[le:ri, up:do])\n",
    "        max_h = torch.max(max_h, torch.as_tensor(item.z + item.height))\n",
    "        new_state[le:ri, up:do] = max_h.clone()\n",
    "        return new_state\n",
    "\n",
    "    def check_bin(self, goods, state):\n",
    "        #货箱的长宽高\n",
    "        Length = self.length\n",
    "        Width = self.width\n",
    "        Height = self.height\n",
    "\n",
    "        # 判断货物本身是否不能放入货箱\n",
    "        if goods.length > Length or goods.width > Width or goods.height > Height:\n",
    "            return -1\n",
    "        # 判断假如货物放入后是否不能超出货箱范围\n",
    "        elif goods.x + goods.length < 0 or goods.y + goods.width > Width or goods.height + state[goods.x][\n",
    "            goods.y] > Height or goods.x + goods.length > Length:\n",
    "            return -1\n",
    "        else:\n",
    "            qwe = 0\n",
    "            for i in range(int(goods.x), int(goods.x + goods.length)):\n",
    "                if qwe == 1:\n",
    "                    break\n",
    "                for j in range(int(goods.y), int(goods.y + goods.width)):\n",
    "                    if state[i][j] > goods.z or goods.height + state[i][j] > Height:\n",
    "                        return -1\n",
    "\n",
    "        # 判断货物是否放置的稳定\n",
    "        point_left_up_1 = goods.x\n",
    "        point_left_up_2 = goods.y\n",
    "        point_left_down_1 = goods.x + goods.length\n",
    "        point_left_down_2 = point_left_up_2\n",
    "        point_right_up_1 = point_left_up_1\n",
    "        point_right_up_2 = point_left_up_2 + goods.width\n",
    "        point_right_down_1 = point_right_up_1 + goods.length\n",
    "        point_right_down_2 = point_right_up_2\n",
    "        if point_right_down_2 < 0 or point_right_down_1 < 0:\n",
    "            return -1\n",
    "        if point_right_up_2 > Width or point_left_down_1 < 0:\n",
    "            return -1\n",
    "        # 计算货物底部被支持的区域面积\n",
    "        max = goods.z\n",
    "        sums = 0.0\n",
    "        # 寻找放置区域内支撑位置个数\n",
    "        for i in range(int(point_left_up_1), int(point_left_down_1)):\n",
    "            for j in range(int(point_left_up_2), int(point_right_up_2)):\n",
    "                if state[i][j] == max:\n",
    "                    sums = sums + 1\n",
    "        k = sums  # 统计当前区域内最高的格子个数\n",
    "        area = k / (1.0 * (int(point_left_down_1) - int(point_left_up_1)) * (\n",
    "                    int(point_right_up_2) - int(point_left_up_2)))\n",
    "        # 获取有几个顶点的高度相同;\n",
    "        if int(point_left_down_1) >= Length:\n",
    "            point_left_down_1 = point_left_down_1 - 1\n",
    "        if int(point_left_down_2) >= Width:\n",
    "            point_left_down_2 = point_left_down_2 - 1\n",
    "        if int(point_right_up_1) >= Length:\n",
    "            point_right_up_1 = point_right_up_1 - 1\n",
    "        if int(point_left_up_2) >= Width:\n",
    "            point_left_up_2 = point_left_up_2 - 1\n",
    "        if int(point_right_up_2) >= Width:\n",
    "            point_right_up_2 = point_right_up_2 - 1\n",
    "        if int(point_left_up_1) >= Length:\n",
    "            point_left_up_1 = point_left_up_1 - 1\n",
    "        if int(point_right_down_1) >= Length:\n",
    "            point_right_down_1 = point_right_down_1 - 1\n",
    "        if int(point_right_down_2) >= Width:\n",
    "            point_right_down_2 = point_right_down_2 - 1\n",
    "        heigh_list = []\n",
    "        heigh_list.append(state[int(point_left_down_1)][int(point_left_down_2)])\n",
    "        heigh_list.append(state[int(point_right_down_1)][int(point_right_down_2)])\n",
    "        heigh_list.append(state[int(point_right_up_1)][int(point_right_up_2)])\n",
    "        heigh_list.append(state[int(point_left_up_1)][int(point_left_up_2)])\n",
    "        number = np.bincount(heigh_list)\n",
    "        sum = np.max(number)\n",
    "        # 判断能否被支持，可以返回1，不能则返回-1\n",
    "        if sum == 3:\n",
    "            if area > 0.55:\n",
    "                return 1\n",
    "            else:\n",
    "                return -1\n",
    "        elif sum == 4:\n",
    "            if area > 0.4:\n",
    "                return 1\n",
    "            else:\n",
    "                return -1\n",
    "        else:\n",
    "            if area > 0.6:\n",
    "                return 1\n",
    "            else:\n",
    "                return -1\n",
    "\n",
    "    def check_bin_below_max_z(self, goods, state, max_z):\n",
    "        #货箱的长宽高\n",
    "        Length = self.length\n",
    "        Width = self.width\n",
    "        Height = self.height\n",
    "        #判断是否超过自限定最大高\n",
    "        if goods.height + goods.z >max_z:\n",
    "            return -1\n",
    "        # 判断货物本身是否不能放入货箱\n",
    "        if goods.length > Length or goods.width > Width or goods.height > Height:\n",
    "            return -1\n",
    "        # 判断假如货物放入后是否不能超出货箱范围\n",
    "        elif goods.x + goods.length < 0 or goods.y + goods.width > Width or goods.height + state[goods.x][\n",
    "            goods.y] > Height or goods.x + goods.length > Length:\n",
    "            return -1\n",
    "        else:\n",
    "            qwe = 0\n",
    "            for i in range(int(goods.x), int(goods.x + goods.length)):\n",
    "                if qwe == 1:\n",
    "                    break\n",
    "                for j in range(int(goods.y), int(goods.y + goods.width)):\n",
    "                    if state[i][j] > goods.z or goods.height + state[i][j] > Height:\n",
    "                        return -1\n",
    "\n",
    "        # 判断货物是否放置的稳定\n",
    "        point_left_up_1 = goods.x\n",
    "        point_left_up_2 = goods.y\n",
    "        point_left_down_1 = goods.x + goods.length\n",
    "        point_left_down_2 = point_left_up_2\n",
    "        point_right_up_1 = point_left_up_1\n",
    "        point_right_up_2 = point_left_up_2 + goods.width\n",
    "        point_right_down_1 = point_right_up_1 + goods.length\n",
    "        point_right_down_2 = point_right_up_2\n",
    "        if point_right_down_2 < 0 or point_right_down_1 < 0:\n",
    "            return -1\n",
    "        if point_right_up_2 > Width or point_left_down_1 < 0:\n",
    "            return -1\n",
    "        # 计算货物底部被支持的区域面积\n",
    "        max = goods.z\n",
    "        sums = 0.0\n",
    "        # 寻找放置区域内支撑位置个数\n",
    "        for i in range(int(point_left_up_1), int(point_left_down_1)):\n",
    "            for j in range(int(point_left_up_2), int(point_right_up_2)):\n",
    "                if state[i][j] == max:\n",
    "                    sums = sums + 1\n",
    "        k = sums  # 统计当前区域内最高的格子个数\n",
    "        area = k / (1.0 * (int(point_left_down_1) - int(point_left_up_1)) * (\n",
    "                    int(point_right_up_2) - int(point_left_up_2)))\n",
    "        # 获取有几个顶点的高度相同;\n",
    "        if int(point_left_down_1) >= Length:\n",
    "            point_left_down_1 = point_left_down_1 - 1\n",
    "        if int(point_left_down_2) >= Width:\n",
    "            point_left_down_2 = point_left_down_2 - 1\n",
    "        if int(point_right_up_1) >= Length:\n",
    "            point_right_up_1 = point_right_up_1 - 1\n",
    "        if int(point_left_up_2) >= Width:\n",
    "            point_left_up_2 = point_left_up_2 - 1\n",
    "        if int(point_right_up_2) >= Width:\n",
    "            point_right_up_2 = point_right_up_2 - 1\n",
    "        if int(point_left_up_1) >= Length:\n",
    "            point_left_up_1 = point_left_up_1 - 1\n",
    "        if int(point_right_down_1) >= Length:\n",
    "            point_right_down_1 = point_right_down_1 - 1\n",
    "        if int(point_right_down_2) >= Width:\n",
    "            point_right_down_2 = point_right_down_2 - 1\n",
    "        heigh_list = []\n",
    "        heigh_list.append(state[int(point_left_down_1)][int(point_left_down_2)])\n",
    "        heigh_list.append(state[int(point_right_down_1)][int(point_right_down_2)])\n",
    "        heigh_list.append(state[int(point_right_up_1)][int(point_right_up_2)])\n",
    "        heigh_list.append(state[int(point_left_up_1)][int(point_left_up_2)])\n",
    "        number = np.bincount(heigh_list)\n",
    "        sum = np.max(number)\n",
    "        # 判断能否被支持，可以返回1，不能则返回-1\n",
    "        if sum == 3:\n",
    "            if area > 0.55:\n",
    "                return 1\n",
    "            else:\n",
    "                return -1\n",
    "        elif sum == 4:\n",
    "            if area > 0.4:\n",
    "                return 1\n",
    "            else:\n",
    "                return -1\n",
    "        else:\n",
    "            if area > 0.6:\n",
    "                return 1\n",
    "            else:\n",
    "                return -1\n",
    "\n",
    "    #获取货物x,y坐标，二维角度\n",
    "    def idx_to_position(self, idx):\n",
    "        # 取整除 - 返回商的整数部分（向下取整）\n",
    "        x = torch.div(idx, self.width, rounding_mode='trunc')\n",
    "        y = idx % self.width\n",
    "        return int(x), int(y)\n",
    "\n",
    "    def packer_family_stable_2(self, car_list_sort_fixed, box_sort_list):\n",
    "        #取消可装载点时取消循环或者递归\n",
    "        #family:类别记录\n",
    "        #具有稳定性的放置\n",
    "        temp_l = 0    # 临时长，这是装入每个箱子的参照\n",
    "        temp_w = 0\n",
    "        temp_h = 0\n",
    "        temp_ll = 0\n",
    "        temp_ww = 0\n",
    "        temp_hh = 0\n",
    "        c = 0\n",
    "        i_volume = 0\n",
    "        b_volume = 0\n",
    "        item_list = []\n",
    "        peritem_list = []\n",
    "        family_list = []\n",
    "        self.init_height_graph(car_list_sort_fixed[0])\n",
    "        #for循环所有货箱 i:Car类\n",
    "        for i in car_list_sort_fixed:\n",
    "            self.width = i.width\n",
    "            self.height = i.height\n",
    "            self.length = i.length\n",
    "            #_break_1 = True\n",
    "            max_z = 0\n",
    "            for kkk in range(2):\n",
    "                if i.position == [0, 0, 0]:  # 这里绝对不是i.position\n",
    "                    if ((box_sort_list[0].height > i.height) or (\\\n",
    "                                    box_sort_list[0].width > i.width) or ( \\\n",
    "                                    box_sort_list[0].length > i.length)):\n",
    "                        break\n",
    "                        # 这个意思就是说如果最开始就放不进去就不要放了\n",
    "                    elif (\n",
    "                            (box_sort_list[0].height <= i.height) and (\\\n",
    "                                    box_sort_list[0].width <= i.width) and (\\\n",
    "                                            box_sort_list[0].length <= i.length)\n",
    "                    ):\n",
    "                        first_item = box_sort_list[0]\n",
    "                        # first_item.length = d[Attr.Axis_x]\n",
    "                        # first_item.width = d[Attr.Axis_y]\n",
    "                        # first_item.height = d[Attr.Axis_z]\n",
    "                        #print(first_item.length, first_item.width, first_item.height)\n",
    "                        first_item.x, first_item.y, first_item.z = 0, 0, 0\n",
    "                        i.position = [first_item.length, first_item.width, first_item.height]\n",
    "                        #更新高度图\n",
    "                        self.height_graph = self.update_height_graph(first_item, self.height_graph)\n",
    "                        #可行性掩码的最大高度限制\n",
    "                        max_z = first_item.height\n",
    "                        #删除内存\n",
    "                        item_list.append(box_sort_list[0])\n",
    "                        peritem_list.append(box_sort_list[0].volume/i.volume)\n",
    "                        i_volume += box_sort_list[0].volume\n",
    "                        c += 1\n",
    "                        family_list.append(box_sort_list[0].family)\n",
    "                        box_sort_list.pop(0)\n",
    "                        continue\n",
    "                    # 以上是空车第一次放置的时候\n",
    "                else:\n",
    "                    _break = False\n",
    "                    for j in box_sort_list[0:]: #原[1:]\n",
    "                        temp_box = PackingMethod.set_pose(j, PackingMethod.pose_fit_arg(j, temp_l, temp_w,temp_h)).get_dimension()\n",
    "                        item = j\n",
    "                        item.length = temp_box[Attr.Axis_x]\n",
    "                        item.width = temp_box[Attr.Axis_y]\n",
    "                        item.height = temp_box[Attr.Axis_z]\n",
    "                        #获取货物的可行掩码矩阵\n",
    "                        real_mask = self.real_mask_below_max_z(self.height_graph, item, max_z)\n",
    "                        #print(real_mask)\n",
    "                        if torch.sum(real_mask) != 0. :\n",
    "                            #取可行性掩码中第一个为1的位置作为放置点\n",
    "                            position_xyz = torch.argmax(real_mask)\n",
    "                            item.x, item.y = self.idx_to_position(position_xyz)\n",
    "                            item.z = self.height_graph[item.x, item.y]\n",
    "                            self.height_graph = self.update_height_graph(item, self.height_graph)\n",
    "                            max_z = torch.max(max_z, item.z + item.height)\n",
    "                        elif torch.sum(real_mask) == 0.:\n",
    "                            #挑出循环体\n",
    "                            _break = True\n",
    "                            print(\"装载满了！！\")\n",
    "                            break\n",
    "                        # if _break:\n",
    "                        #     break\n",
    "            b_volume = i.volume\n",
    "        return i_volume / b_volume, c, item_list, family_list\n",
    "\n",
    "    def real_mask(self, state, item):\n",
    "        truth_mask = torch.zeros([self.length, self.width], dtype=torch.uint8)\n",
    "        #predict_mask_length = predict_mask.shape[1]\n",
    "        lx = int(item.length.item())\n",
    "        ly = int(item.width.item())\n",
    "        lz = int(item.height.item())\n",
    "        #jund_0001 = torch.tensor(0.001)\n",
    "        #jund_0 = torch.tensor(0.0)\n",
    "        for i in range(self.width * self.length):\n",
    "            action_x, action_y = self.idx_to_position(i)\n",
    "            #mask_item = predict_mask[0, i]\n",
    "            # 货箱放置的x,y缺一个z\n",
    "            # 要根据state判断这个点能不能装进去这个货物\n",
    "            #name,length,width,height,weight,pose,family\n",
    "            item = Bin(\"\", lx, ly, lz, 0, 0, 0)\n",
    "            item.x = action_x\n",
    "            item.y = action_y\n",
    "            item.z = self.height_graph[action_x, action_y]\n",
    "            if self.check_bin(item, state) != -1:\n",
    "                truth_mask[action_x, action_y] = 1\n",
    "        return truth_mask\n",
    "    def real_mask_below_max_z(self, state, item, max_z):\n",
    "        #在最大z的限制之下，确定可行性掩码\n",
    "        truth_mask = torch.zeros([self.length, self.width], dtype=torch.uint8)\n",
    "        #predict_mask_length = predict_mask.shape[1]\n",
    "        lx = int(item.length.item())\n",
    "        ly = int(item.width.item())\n",
    "        lz = int(item.height.item())\n",
    "        #jund_0001 = torch.tensor(0.001)\n",
    "        #jund_0 = torch.tensor(0.0)\n",
    "        for i in range(self.width * self.length):\n",
    "            action_x, action_y = self.idx_to_position(i)\n",
    "            #mask_item = predict_mask[0, i]\n",
    "            # 货箱放置的x,y缺一个z\n",
    "            # 要根据state判断这个点能不能装进去这个货物\n",
    "            #name,length,width,height,weight,pose,family\n",
    "            item = Bin(\"\", lx, ly, lz, 0, 0, 0)\n",
    "            item.x = action_x\n",
    "            item.y = action_y\n",
    "            item.z = self.height_graph[action_x, action_y]\n",
    "            if self.check_bin_below_max_z(item, state, max_z) != -1:\n",
    "                truth_mask[action_x, action_y] = 1\n",
    "        return truth_mask\n",
    "\n",
    "class Bin_env:\n",
    "    def __init__(self, items, prob, index,\n",
    "                 carlist):  # param:items:itemlist:data_create()的列表, problist, indexlist, carlist\n",
    "        self.Reward_list = []\n",
    "        self.Index_list = []\n",
    "        self.State_list = []\n",
    "        self.prob_list = []\n",
    "        self.peritem_list = []\n",
    "        self.carlist = carlist\n",
    "        load_items = Creator.bin_create(items, index)  # 装箱\n",
    "        # rate 占有率\n",
    "        # num:\n",
    "        # item_list\n",
    "        #\n",
    "        rate, num, item_list, peritem_list = Packer().packer_own(carlist, load_items)\n",
    "        for z in item_list:\n",
    "            self.Reward_list.append(z.volume)\n",
    "        for x in range(len(item_list)):\n",
    "            self.State_list.append(x)\n",
    "            self.Index_list.append(index[x][0])\n",
    "            self.prob_list.append(prob[x][0])\n",
    "        self.peritem_list = peritem_list\n",
    "        # print(peritem_list)\n",
    "\n",
    "    # 定义方法：\n",
    "    # 作用： 返回当前状态的利用率\n",
    "    def render(self, state):\n",
    "        pass\n",
    "    def reset(self):\n",
    "        pass\n",
    "\n",
    "    def step(self, state):  # 这个动作就是当前的序列\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
